package com.easy.work.function.me;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshListView;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.db.DataProvider;
import com.easy.work.db.MessageBean;
import com.easy.work.function.me.adapter.MessageListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/12/6.
 * 消息
 */
public class MessageListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
    @BindView(R.id.listView)
    PullToRefreshListView refreshListView;
    ListView listView;
    private MessageListAdapter mAdapter;
    private List<MessageBean> mList = new ArrayList<>();

    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, MessageListActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_message);
        ButterKnife.bind(this);
        initTitle("系统消息");

        initView();

        AppSession.setPullFormartRefreshTime(refreshListView);
        refreshListView.doPullRefreshing(true, 0);
    }

    private void initView() {
        List<MessageBean> unreadList = DataProvider.getSystemMsg(this, false);
        if (unreadList != null && !unreadList.isEmpty()) {
            mList.addAll(unreadList);
        }
        List<MessageBean> readList = DataProvider.getSystemMsg(this, true);
        if (readList != null && !readList.isEmpty()) {
            mList.addAll(readList);
        }
        if (mList.isEmpty()) {
            mList.add(DataProvider.getMsgInstance());
        }
        mAdapter = new MessageListAdapter(this, 1, mList);
        refreshListView.setPullLoadEnabled(true);
        refreshListView.setPullRefreshEnabled(true);
        refreshListView.setOnRefreshListener(this);

        listView = refreshListView.getRefreshableView();
        listView.setHeaderDividersEnabled(false);
        listView.setDividerHeight(0);
        listView.setAdapter(mAdapter);

        DataProvider.updateReadSystemMsg(this);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                MessageBean bean = (MessageBean) parent.getAdapter().getItem(position);
                if (!"##".equals(bean.getPhone())) {
                    showNoticeDialog(bean);
                }
                return false;
            }
        });
    }

    private void showNoticeDialog(final MessageBean bean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("温馨提示");
        builder.setMessage("确定要删除该条消息吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DataProvider.deleteData(MessageListActivity.this, bean);
                mList.remove(bean);
                mAdapter.notifyDataSetChanged();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        refreshListView.onPullUpRefreshComplete();
        refreshListView.onPullDownRefreshComplete();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        refreshListView.onPullUpRefreshComplete();
        refreshListView.onPullDownRefreshComplete();
    }
}
